package krow.dev.addetector.view.adapter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import krow.dev.addetector.R;
import krow.dev.addetector.model.ApplicationItem;
import krow.dev.addetector.view.LinkableTextView;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ApplicationListAdapter extends BaseAdapter {

	private ArrayList<ApplicationItem> mApplicationItemList;
	
	private LayoutInflater mLayoutInflater;
	private PackageManager mPackageManager;

	private LinkableTextView.OnLinkClickListener mLinkClickListener;
	
	private static SimpleDateFormat sSimpleDateFormat = new SimpleDateFormat("yyyy.MM.dd", Locale.KOREA);

	public ApplicationListAdapter(Context context) {

		mLayoutInflater = LayoutInflater.from(context);
		mPackageManager = context.getPackageManager();

		mApplicationItemList = new ArrayList<ApplicationItem>();
	}

	public void setApplicationItemList(List<ApplicationItem> applicationItemList) {

		mApplicationItemList.clear();
		mApplicationItemList.addAll(applicationItemList);
	}

	public List<ApplicationItem> getApplicationItemList(){
		
		return mApplicationItemList;
	}
	
	public void setOnLinkClickListener(LinkableTextView.OnLinkClickListener linkClickListener) {

		mLinkClickListener = linkClickListener;
	}

	@Override
	public int getCount() {

		return mApplicationItemList.size();
	}

	@Override
	public Object getItem(int position) {

		return mApplicationItemList.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@SuppressLint({ "ViewTag", "InflateParams" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ApplicationItem applicationItem = mApplicationItemList.get(position);

		View view = convertView;
		if (view == null) {
			view = mLayoutInflater.inflate(R.layout.item_application, null);
			view.setTag(R.id.image_thumbnail, view.findViewById(R.id.image_thumbnail));
			view.setTag(R.id.text_title, view.findViewById(R.id.text_title));
			view.setTag(R.id.text_content, view.findViewById(R.id.text_content));
			view.setTag(R.id.text_size, view.findViewById(R.id.text_size));
			view.setTag(R.id.text_date, view.findViewById(R.id.text_date));
			view.setTag(R.id.text_module, view.findViewById(R.id.text_module));
		}
		view.setTag(applicationItem);
		
		PackageInfo packageInfo = applicationItem.getPackageInfo();
		
		File file = new File(packageInfo.applicationInfo.sourceDir);
		
		ImageView thumbnailView = (ImageView) view.getTag(R.id.image_thumbnail);
		thumbnailView.setImageDrawable(packageInfo.applicationInfo.loadIcon(mPackageManager));
		
		TextView nameView = (TextView) view.getTag(R.id.text_title);
		nameView.setText(packageInfo.applicationInfo.loadLabel(mPackageManager));

		TextView contentView = (TextView) view.getTag(R.id.text_content);
		contentView.setText(packageInfo.packageName);
		
		TextView sizeView = (TextView) view.getTag(R.id.text_size);
		sizeView.setText(getFileSize(file));

		TextView dateView = (TextView) view.getTag(R.id.text_date);
		dateView.setText(getLastModifiedDate(file));
		
		LinkableTextView moduleView = (LinkableTextView) view.getTag(R.id.text_module);
		moduleView.setOnLinkClickListener(mLinkClickListener);
		moduleView.gatherLinksForText(applicationItem.getModuleList());
		
		return view;
	}

	public static String getFileSize(File file) {
		long fileSize = 0;
		if (file.exists()) {
			try {
				fileSize = file.length();
			} catch (Exception e) {

			}
		}
		
		String unitArray[] = { "Byte", "KB", "MB" };
		String returnSize = new String();
		int index = 0;
		double changeSize = 0;
		try {
			for (int i = 0; (fileSize / (double) 1024) > 0; i++, fileSize /= (double) 1024) {
				index = i;
				changeSize = fileSize;
			}
			returnSize = changeSize + unitArray[index];
		} catch (Exception ex) {
			returnSize = "0.0 Byte";
		}
		return returnSize;
	}

	private static String getLastModifiedDate(File file) {
		String lastModifiedDate = null;

		if (file.exists()) {
			try {
				lastModifiedDate = sSimpleDateFormat.format(new Date(file.lastModified()));
			} catch (Exception e) {

			}
		}
		return lastModifiedDate;
	}
}
